/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.server;

import java.util.ArrayList;
import java.util.TreeMap;

import qurtext.client.ClientChapter;
import qurtext.client.ClientSection;
import qurtext.client.ClientUser;
import qurtext.client.ClientVerse;
import qurtext.client.QurTextService;
import qurtext.domain.Chapter;
import qurtext.domain.Profile;
import qurtext.domain.Section;
import qurtext.domain.Verse;
import qurtext.factory.ChapterFactory;
import qurtext.factory.ProfileFactory;
import qurtext.factory.SectionFactory;
import qurtext.factory.VerseFactory;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class QurTextServiceImpl extends RemoteServiceServlet implements
		QurTextService {

	private ChapterFactory chapterFactory;
	private SectionFactory sectionFactory;
	private UserService userService;
	private VerseFactory verseFactory;
	private ProfileFactory profileFactory;
	
	public QurTextServiceImpl() {
		super();
		chapterFactory = new ChapterFactory();
		sectionFactory = new SectionFactory();
		verseFactory = new VerseFactory();
		userService = UserServiceFactory.getUserService();
		profileFactory = new ProfileFactory();
	}

	public TreeMap<Integer,ClientChapter> getAllChapterList() {
		TreeMap<Integer,ClientChapter> resultMap = new TreeMap<Integer,ClientChapter>();
		for (Chapter chapter:chapterFactory.getAllChapterList()) {
			resultMap.put(chapter.getChapterNo(),new ClientChapter(chapter.getChapterNo(),chapter.getSectionCount(), chapter.getTitle(),chapter.getTransliteration()));
		}
		return resultMap;
	}

	public int initChapterList(int start) {		
		return chapterFactory.initChapterList(start);
	}

	public ClientUser getCurrentUser(String destinationURL) {
		if (userService.isUserLoggedIn()) {
			User user = userService.getCurrentUser();
			Profile profile = profileFactory.getProfile(user.getEmail());
			return new ClientUser(user.getEmail(),userService.createLogoutURL(destinationURL), userService.isUserAdmin() || user.getEmail().equals("aburizal"+"@gmail.com"), profile.getSectionRead(), profile.getCurrentHistory());
		} else {
			return new ClientUser(null,userService.createLoginURL(destinationURL), false, null, null);
		}
	}

	@Override
	public TreeMap<Integer, ClientVerse> getSectionVerseList(int chapterNo, int verseNo, int endVerseNo) {
		TreeMap<Integer, ClientVerse> resultMap = new TreeMap<Integer, ClientVerse>();
		for (Verse verse:verseFactory.getSectionVerseList(chapterNo, verseNo, endVerseNo)) {
			resultMap.put(verse.getVerseNo(),new ClientVerse(chapterNo,verse.getVerseNo(),verse.getUthmani(),verse.getLiteral(),verse.getTransliteration(),verse.getTranslation(),verse.getTopics()));
		}
		return resultMap;
	}

	@Override
	public void initSectionVerses(int chapterNo, int verseNo) {
		sectionFactory.initSectionVerses(chapterNo, verseNo);
	}

	@Override
	public ArrayList<ClientSection> getAllSectionList() {
		ArrayList<ClientSection> resultList = new ArrayList<ClientSection>();
		for (Section section:sectionFactory.getAllSectionList()) {
			resultList.add(new ClientSection(section.getSectionNo(),section.getChapterNo(),section.getStartVerse(),section.getEndVerse()));
		}
		return resultList;
	}

	@Override
	public ClientVerse getBasmallah() {
		Verse result = verseFactory.getBasmallah();
		if (null==result) return new ClientVerse();
		return new ClientVerse(result.getChapterNo(),result.getVerseNo(),result.getUthmani(),result.getLiteral(),result.getTransliteration(),result.getTranslation(),result.getTopics());
	}

	@Override
	public void saveProgress(ClientUser profile) {
		profileFactory.saveProgress(profile.email,profile.getSectionReadCookie(),profile.lastSection);
	}

	@Override
	public String initSectionLiterals(String start) {
		return sectionFactory.initSectionLiterals(start);
	}

	@Override
	public String initAllLiterals(String start) {
		String result=sectionFactory.initSectionLiterals(start);
		if (result!=null) return result;
		String[] location=start.split("[:]");
		int chapterNo=Integer.valueOf(location[0]);
		int verseNo=Integer.valueOf(location[1]);
		if (chapterNo==114 && verseNo==6) return null;//truly end
		Section section=sectionFactory.getSection(chapterNo, verseNo+1);
		if (null==section) section=sectionFactory.getSection(chapterNo+1, 1);		
		return ""+section.getChapterNo()+":"+section.getStartVerse()+":0";
	}

}
